package com.irdstudio.bsp.executor.core.plugin.ftp;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.irdstudio.bsp.executor.core.plugin.AbstractPlugin;

/**
 * FTP应用插件,可将指定的文件上传到指定的FTP服务器
 * 或下载指定服务器上的文件到本地的目录
 * @author guangming.li
 * @date 2013-11-12
 * @version 1.0
 */
public class FtpPlugin extends AbstractPlugin {

	/* 表数据装配配置列表 */
	private List<PluginFtpConf> ftpList = null;
	
	public boolean execute() {
		this.logger.info("........................................................................");
		boolean flag = true ;
		SftpClient sc = null ;
		FtpClient fc = null ;
		try {
			//循环FTP配置参数
			for (PluginFtpConf pfc : ftpList) {
				// 解释数据导出配置中的变量(支持系统变量系统中的变量)
				pfc.setRemoteFileName(context.toParseSysVariable(pfc.getRemoteFileName()));
				pfc.setFtpFilePath(context.toParseSysVariable(pfc.getFtpFilePath()));
				pfc.setLocalFileName(context.toParseSysVariable(pfc.getLocalFileName()));
				pfc.setLocalFilePath(context.toParseSysVariable(pfc.getLocalFilePath()));
				
				//调用服务类型
				String serverType = pfc.getServerType();
				//服务IP地址
				String sftpServerIp = pfc.getSftpServerIp();
				//服务端口
				int ftpServerPort = pfc.getFtpServerPort();
				//用户名
				String ftpServerUser = pfc.getFtpServerUser();
				//密码
				String ftpServerPwd = pfc.getFtpServerPwd();
				//操作类型
				String loadType = pfc.getLoadType();
				//服务器文件名称
				String remoteFileName = pfc.getRemoteFileName();
				// 本地文件名称
				String localFileName = pfc.getLocalFileName();
				//服务器文件路径
				String ftpFilePath = pfc.getFtpFilePath();
				//本地路径
				String localFilePath = pfc.getLocalFilePath();
				
				if(FtpPluginContants.SERVER_SFTP.equals(serverType)) {
					//连接SFTP数据库
					sc = new SftpClient(sftpServerIp, ftpServerPort, ftpServerUser, ftpServerPwd,this.logger);
					if(FtpPluginContants.UPLOAD.equals(loadType)) {
						logger.info("文件[" + localFilePath+localFileName+ "]上传，服务器["+ftpFilePath+"]开始");
						sc.upLoad(localFileName, ftpFilePath, localFilePath);
					}else if(FtpPluginContants.DOWNLOAD.equals(loadType)) {
						logger.info("文件[" + localFilePath+localFileName+ "]下载，服务器["+ftpFilePath+"]开始");
						sc.downLoad(remoteFileName, ftpFilePath, localFilePath);
					}else {
						throw new Exception("文件操作类型为空！！！！");
					}
				}else if(FtpPluginContants.SERVER_FTP.equals(serverType)) {
					fc = new FtpClient();
					//打开FTP
					fc.openSession(sftpServerIp, ftpServerPort, ftpServerUser, ftpServerPwd);
					if(FtpPluginContants.UPLOAD.equals(loadType)) {
						logger.info("文件[" + localFilePath+localFileName+ "]上传，服务器["+ftpFilePath+"]开始");
						fc.uploadFile(localFilePath+localFileName, ftpFilePath);
					}else if(FtpPluginContants.DOWNLOAD.equals(loadType)) {
						logger.info("文件[" + localFilePath+localFileName+ "]下载，服务器["+ftpFilePath+"]开始");
						fc.downloadFile(ftpFilePath+remoteFileName, localFilePath);
					}else {
						throw new Exception("文件操作类型为空！！！！");
					}
				}
			}
		} catch (Exception e) {
			flag = false;
			logger.error("FTP插件执行失败：" + e.getMessage(), e);
			context.setSzLastErrorMsg(e.getMessage());
			logger.info("错误信息："+e);
		}finally {
			
		}
		
		return flag;
	}

	@Override
	protected boolean doReadConfigureFromDB(Connection conn,
			String szConfIdentify) throws SQLException {
		
		PluginFtpConfDao pfcDao = new PluginFtpConfDao(conn);
		ftpList = pfcDao.queryWithCond(" where plugin_conf_id='"
				+ szConfIdentify + "'", "order by conf_sort");
		if (ftpList.size() < 1) {
			context.setSzLastErrorMsg("未读取到配置标识为：" + szConfIdentify + "的数据FTP配置!");
			return false;
		} else {
			return true;
		}
	}

}
